package com.example.narratejpa.impl;

import com.example.narratejpa.entiy.Region;
import com.example.narratejpa.repository.RegionRepository;
import com.example.narratejpa.service.RegionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class RegionServiceImpl implements RegionService {
    @Autowired
    private RegionRepository regionRepository;

    @Override
    public Page<Region> getPage(Integer pageNum, Integer pageLimit) {
        Pageable pageable = new PageRequest(pageNum - 1, pageLimit);
        return regionRepository.findAll(pageable);
    }

    @Override
    public Region addRegion(Region region) {
        Region s = regionRepository.save(region);
        return s;
    }

    @Override
    public void delRegionByID(Integer id) {
        regionRepository.deleteById(id);
    }

    @Override
    public Region findRegionByID(Integer id) {
        Optional<Region> regionOpt = regionRepository.findById(id);
        Region region = regionOpt.get();
        return region;
    }

    @Override
    public Region updateRegion(Region region) {
        Region region1 = regionRepository.save(region);
        return region1;
    }

    @Override
    public void batchDelectRegion(List<Integer> list) {
       regionRepository.deleteBatch(list);
    }


}
